 
package com.zdkj.subway.rca.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.rca.entity.SwCkStatementConfirmAirQrConsumeTmp;
import com.zdkj.subway.rca.entity.SwCkStatementConfirmAirQrConsume;
import com.zdkj.subway.rca.mapper.SwCkStatementConfirmAirQrConsumeTmpMapper;
import com.zdkj.subway.rca.service.SwCkStatementConfirmAirQrConsumeTmpService;
import com.zdkj.subway.rca.service.SwCkStatementConfirmAirQrConsumeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.ZoneId;
import java.util.*;

/**
 * 西安地铁城际二维码消费过闸确认临时表
 *
 * @author stw
 * @date 2020-10-25 10:20:30
 */
@Service
public class SwCkStatementConfirmAirQrConsumeTmpServiceImpl extends ServiceImpl<SwCkStatementConfirmAirQrConsumeTmpMapper, SwCkStatementConfirmAirQrConsumeTmp> implements SwCkStatementConfirmAirQrConsumeTmpService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SwCkStatementCatSubwayconsumeTmpServiceImpl.class);
    private String parsePattern = "yyyy-MM-dd HH:mm:ss";
    @Autowired
    private SwCkStatementConfirmAirQrConsumeService swCkStatementConfirmAirQrConsumeService;

    @Override
    public Boolean saveBatchWithExcel(final File excelFile) throws Exception {
        List<Map<Integer, String>> listMap = EasyExcel.read(excelFile).sheet().headRowNumber(3).doReadSync();
        if (CollectionUtil.isNotEmpty(listMap)) {
            List<SwCkStatementConfirmAirQrConsumeTmp> listDb = new ArrayList<>();
            MultipartFileToFile.delteTempFile(excelFile);
            for (int m = 0; m < listMap.size(); m++) {
                Map<Integer, String> data = listMap.get(m);
                // 返回每条数据的键值对 表示所在的列 和所在列的值
                LOGGER.info("读取到数据:{}", JSON.toJSONString(data));

                SwCkStatementConfirmAirQrConsumeTmp tmpObj = new SwCkStatementConfirmAirQrConsumeTmp();
                tmpObj.setTradeDateTmp(data.get(0));//交易日期

                tmpObj.setAlipayTotalTmp(data.get(1));
                tmpObj.setAlipyaSubwayMoneyTmp(data.get(2));
                tmpObj.setAlipyaAirMoneyTmp(data.get(3));

                tmpObj.setWechatTotalTmp(data.get(4));
                tmpObj.setWechatSubwayMoneyTmp(data.get(5));
                tmpObj.setWechatAirMoneyTmp(data.get(6));

                tmpObj.setBankTotalTmp(data.get(7));
                tmpObj.setBankSubwayMoneyTmp(data.get(8));
                tmpObj.setBankAirMoneyTmp(data.get(9));

                tmpObj.setBankQuickTotalTmp(data.get(10));
                tmpObj.setBankQuickSubwayMoneyTmp(data.get(11));
                tmpObj.setBankQuickAirMoneyTmp(data.get(12));

                if (StringUtils.isEmpty(tmpObj.getTradeDateTmp())
                        || StringUtils.isEmpty(tmpObj.getAlipayTotalTmp())
                        || StringUtils.isEmpty(tmpObj.getWechatTotalTmp())
                        || StringUtils.isEmpty(tmpObj.getBankTotalTmp())
                        || StringUtils.isEmpty(tmpObj.getBankQuickTotalTmp())
                        ) {
                    continue;
                }
                if (!parseDate(tmpObj.getTradeDateTmp())) {
                    continue;
                }

                final SwCkStatementConfirmAirQrConsumeTmp obj = ObjectUtils.getDefaultObj(SwCkStatementConfirmAirQrConsumeTmp.class);
                BeanUtils.copyProperties(tmpObj, obj, "deptId", "creatorId", "creatorName");
                checkErr(obj);
                listDb.add(obj);

            }
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("CREATOR_ID", listDb.get(0).getCreatorId());
            this.baseMapper.deleteByMap(columnMap);
            return this.saveBatch(listDb);

        }

        return false;
    }

    private void checkErr(SwCkStatementConfirmAirQrConsumeTmp obj) {
        //更新字段
        String tradeDateTmp = obj.getTradeDateTmp();
        try {
            Date tradeDate = DateUtils.parseDate(tradeDateTmp, parsePattern);
            obj.setTradeDate(tradeDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        } catch (Exception e) {
            obj.setErrMsg("交易日期 为空或格式错误");
        }


        try {
            BigDecimal num = new BigDecimal("100");

            String alipayTotalTmp = obj.getAlipayTotalTmp();
            BigDecimal alipayTotal = new BigDecimal(alipayTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            alipayTotal = alipayTotal.multiply(num);
            obj.setAlipayTotal(alipayTotal.longValue());

            String alipyaSubwayMoneyTmp = obj.getAlipyaSubwayMoneyTmp();
            BigDecimal alipyaSubwayMoney = new BigDecimal(alipyaSubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            alipyaSubwayMoney = alipyaSubwayMoney.multiply(num);
            obj.setAlipyaSubwayMoney(alipyaSubwayMoney.longValue());

            String alipyaAirMoneyTmp = obj.getAlipyaAirMoneyTmp();
            BigDecimal alipyaAirMoney = new BigDecimal(alipyaAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            alipyaAirMoney = alipyaAirMoney.multiply(num);
            obj.setAlipyaAirMoney(alipyaAirMoney.longValue());

            String wechatTotalTmp = obj.getWechatTotalTmp();
            BigDecimal wechatTotal = new BigDecimal(wechatTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            wechatTotal = wechatTotal.multiply(num);
            obj.setWechatTotal(wechatTotal.longValue());

            String wechatSubwayMoneyTmp = obj.getWechatSubwayMoneyTmp();
            BigDecimal wechatSubwayMoney = new BigDecimal(wechatSubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            wechatSubwayMoney = wechatSubwayMoney.multiply(num);
            obj.setWechatSubwayMoney(wechatSubwayMoney.longValue());

            String wechatAirMoneyTmp = obj.getWechatAirMoneyTmp();
            BigDecimal wechatAirMoney = new BigDecimal(wechatAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            wechatAirMoney = wechatAirMoney.multiply(num);
            obj.setWechatAirMoney(wechatAirMoney.longValue());

            String bankTotalTmp = obj.getBankTotalTmp();
            BigDecimal bankTotal = new BigDecimal(bankTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankTotal = bankTotal.multiply(num);
            obj.setBankTotal(bankTotal.longValue());

            String bankSubwayMoneyTmp = obj.getBankSubwayMoneyTmp();
            BigDecimal bankSubwayMoney = new BigDecimal(bankSubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankSubwayMoney = bankSubwayMoney.multiply(num);
            obj.setBankSubwayMoney(bankSubwayMoney.longValue());

            String bankAirMoneyTmp = obj.getBankAirMoneyTmp();
            BigDecimal bankAirMoney = new BigDecimal(bankAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankAirMoney = bankAirMoney.multiply(num);
            obj.setBankAirMoney(bankAirMoney.longValue());

            String bankQuickTotalTmp = obj.getBankQuickTotalTmp();
            BigDecimal bankQuickTotal = new BigDecimal(bankQuickTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankQuickTotal = bankQuickTotal.multiply(num);
            obj.setBankQuickTotal(bankQuickTotal.longValue());

            String bankQuickSubwayMoneyTmp = obj.getBankQuickSubwayMoneyTmp();
            BigDecimal bankQuickSubwayMoney = new BigDecimal(bankQuickSubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankQuickSubwayMoney = bankQuickSubwayMoney.multiply(num);
            obj.setBankQuickSubwayMoney(bankQuickSubwayMoney.longValue());

            String bankQuickAirMoneyTmp = obj.getBankQuickAirMoneyTmp();
            BigDecimal bankQuickAirMoney = new BigDecimal(bankQuickAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bankQuickAirMoney = bankQuickAirMoney.multiply(num);
            obj.setBankQuickAirMoney(bankQuickAirMoney.longValue());
        } catch (Exception e) {
            obj.setErrMsg("金额 为空或格式错误");
        }


        //判断数据是否存在
        SwCkStatementConfirmAirQrConsume SwCkStatementConfirmAirQrConsume = new SwCkStatementConfirmAirQrConsume();
        BeanUtils.copyProperties(obj, SwCkStatementConfirmAirQrConsume, "deptId","creatorId","creatorName","createTime");
        List<SwCkStatementConfirmAirQrConsume> list = swCkStatementConfirmAirQrConsumeService.list(Wrappers.query(SwCkStatementConfirmAirQrConsume));
        if (list != null && !list.isEmpty()) {
            obj.setErrMsg("数据已存在");
        }
    }

    private boolean parseDate(String string) {
        boolean convertSuccess = true;
        if (string == null) {
            return false;
        }

        try {
            DateUtils.parseDate(string, parsePattern);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    @Override
    public void saveBatchWithExcelOk(Integer creatorId) {
        this.baseMapper.saveBatchWithExcelOk(creatorId);
        saveBatchWithExcelCancel(creatorId);
    }

    @Override
    public void saveBatchWithExcelCancel(Integer creatorId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("CREATOR_ID", creatorId);
        this.baseMapper.deleteByMap(columnMap);
    }
}
